<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit;
}

require_once XOOPS_ROOT_PATH . '/core/XCube_ActionForm.class.php';
require_once XOOPS_MODULE_PATH . '/legacy/class/Legacy_Validator.class.php';

/**
 * Mileage_MileEditForm
**/
class Mileage_MileEditForm extends XCube_ActionForm
{
	private $_toUid = NULL;
	/**
	 * getTokenName
	 * 
	 * @param   void
	 * 
	 * @return  string
	**/
	public function getTokenName()
	{
		return "module.mileage.MileEditForm.TOKEN";
	}

	/**
	 * prepare
	 * 
	 * @param   void
	 * 
	 * @return  void
	**/
	public function prepare()
	{
		//
		// Set form properties
		//
		$this->mFormProperties['mile_id']      = new XCube_IntProperty('mile_id');
		$this->mFormProperties['name']         = new XCube_StringProperty('name');
		$this->mFormProperties['point']        = new XCube_IntProperty('point');
		$this->mFormProperties['uid']          = new XCube_IntProperty('uid');
		$this->mFormProperties['reg_unixtime'] = new XCube_IntProperty('reg_unixtime');
		$this->mFormProperties['url']          = new XCube_StringProperty('url');
		$this->mFormProperties['suburl']       = new XCube_IntProperty('suburl');
		$this->mFormProperties['interval']     = new XCube_IntProperty('interval');
		$this->mFormProperties['group']        = new XCube_StringProperty('group');

		//
		// Set field properties
		//
		$this->mFieldProperties['uid'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['uid']->setDependsByArray(array('required'));
		$this->mFieldProperties['uid']->addMessage('required', _MD_MILEAGE_ERROR_REQUIRED, _MD_MILEAGE_LANG_UID);

		$this->mFieldProperties['point'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['point']->setDependsByArray(array('required'));
		$this->mFieldProperties['point']->addMessage('required', _MD_MILEAGE_ERROR_REQUIRED, _MD_MILEAGE_LANG_POINT);

		$this->mFieldProperties['mile_id'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['mile_id']->setDependsByArray(array('required'));
		$this->mFieldProperties['mile_id']->addMessage('required', _MD_MILEAGE_ERROR_REQUIRED, _MD_MILEAGE_LANG_MILE_ID);

		$this->mFieldProperties['name'] = new XCube_FieldProperty($this);
	//	$this->mFieldProperties['name']->setDependsByArray(array('required','maxlength'));
		$this->mFieldProperties['name']->setDependsByArray(array('maxlength'));
		$this->mFieldProperties['name']->addMessage('required', _MD_MILEAGE_ERROR_REQUIRED, _MD_MILEAGE_LANG_NAME);
		$this->mFieldProperties['name']->addMessage('maxlength', _MD_MILEAGE_ERROR_MAXLENGTH, _MD_MILEAGE_LANG_NAME, '255');
		$this->mFieldProperties['name']->addVar('maxlength', '255');

		$this->mFieldProperties['reg_unixtime'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['reg_unixtime']->setDependsByArray(array('required'));
		$this->mFieldProperties['reg_unixtime']->addMessage('required', _MD_MILEAGE_ERROR_REQUIRED, _MD_MILEAGE_LANG_REG_UNIXTIME);

		$this->mFieldProperties['url'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['url']->setDependsByArray(array('required','maxlength'));
		$this->mFieldProperties['url']->addMessage('required', _MD_MILEAGE_ERROR_REQUIRED, _MD_MILEAGE_LANG_URL);
		$this->mFieldProperties['url']->addMessage('maxlength', _MD_MILEAGE_ERROR_MAXLENGTH, _MD_MILEAGE_LANG_URL, '255');
		$this->mFieldProperties['url']->addVar('maxlength', '255');
	}

	public function fetchName()
	{
		$root =& XCube_Root::getSingleton();
		$url = $root->mContext->mRequest->getRequest('url');
		if (strtolower($url) == 'remittance') {
			$toUid = $root->mContext->mRequest->getRequest('toUid');
			if ($uname = Mileage_Utils::getUnameByUid($toUid)) {
				$f_uid = $root->mContext->mXoopsUser->get('uid');
				$f_uname = Mileage_Utils::getUnameByUid($f_uid);
				$fromName = $f_uid .':'. $f_uname;
				$toName = $toUid .':'. $uname;
				$name = sprintf(_MD_MILEAGE_REMITTACE_NAME, $fromName, $toName);
				$post_name = $root->mContext->mRequest->getRequest('name');
				$name .= empty($post_name) ? '': ' (' . preg_replace('/[^a-zA-Z0-9_-]/', '', $post_name) . ')';
				$this->set('name', $name);
			}
		}
	}

	public function fetchInterval()
	{
		$this->set('interval', 0);
	}

	public function fetchUrl()
	{
		$root =& XCube_Root::getSingleton();
		$url = $root->mContext->mRequest->getRequest('url');
		if (strtolower($url) == 'remittance') {
			$toUid = intval($root->mContext->mRequest->getRequest('toUid'));
			$this->set('url', 'remittance:' . $toUid);
		}
	}

	public function fetchUid()
	{
		$root =& XCube_Root::getSingleton();
		if (is_object($root->mContext->mXoopsUser)) {
			$uid = $root->mContext->mXoopsUser->get('uid');
			$this->set('uid', $uid);
		}
	}

	public function fetchReg_unixtime()
	{
		$this->set('reg_unixtime', time());
	}

	public function validateUrl()
	{
		$url = $this->get('url');
		list(,$remit_uid) = explode(':', $url);
		$send_uid = $this->get('uid');

		if ($remit_uid == $send_uid) {
			$this->mErrorFlag = TRUE;
			$this->addErrorMessage(_MD_MILEAGE_REMITTACE_NO_OWN);
		}

		if (strpos(strtolower($url), 'remittance:') !== 0) {
			$this->mErrorFlag = TRUE;
			$this->addErrorMessage(_MD_MILEAGE_ERROR_URL);
		}

		$member_handler =& xoops_gethandler('member');
		$remit_user =& $member_handler->getUser($remit_uid);
		if (!is_object($remit_user)) {
			$this->mErrorFlag = TRUE;
			$this->addErrorMessage(_MD_MILEAGE_REMITTACE_NO_USER);
		}

	}

	public function validatePoint()
	{
		$root =& XCube_Root::getSingleton();
		$dirname = $root->mContext->mRequest->getRequest('dirname');

		$point = $this->get('point');
		$my_totalPoint = Mileage_Utils::totalMileByUid($dirname);
		if ($point < 1 || $point > $my_totalPoint) {
			$this->mErrorFlag = TRUE;
			$this->addErrorMessage(_MD_MILEAGE_ERROR_REMITTANCE_POINT);
		}
	}

	/**
	 * load
	 * 
	 * @param   XoopsSimpleObject  &$obj
	 * 
	 * @return  void
	**/
	public function load(&$obj)
	{
		$this->set('mile_id',      $obj->get('mile_id'));
		$this->set('name',         $obj->get('name'));
		$this->set('point',        $obj->get('point'));
		$this->set('uid',          $obj->get('uid'));
		$this->set('reg_unixtime', $obj->get('reg_unixtime'));
		$this->set('url',          $obj->get('url'));
		$this->set('suburl',       $obj->get('suburl'));
		$this->set('interval',     $obj->get('interval'));
		$this->set('group',        $obj->get('group'));
	}

	/**
	 * update
	 * 
	 * @param   XoopsSimpleObject  &$obj
	 * 
	 * @return  void
	**/
	public function update(&$obj)
	{
		$obj->set('mile_id',      $this->get('mile_id'));
		$obj->set('name',         $this->get('name'));
		$obj->set('point',        $this->get('point'));
		$obj->set('uid',          $this->get('uid'));
		$obj->set('reg_unixtime', $this->get('reg_unixtime'));
		$obj->set('url',          $this->get('url'));
		$obj->set('suburl',       $this->get('suburl'));
		$obj->set('interval',     $this->get('interval'));
		$obj->set('group',        $this->get('group'));
	}

}
